//创建学生相关小仓库
import { defineStore } from "pinia";
import { reqStudentAll, reqStudentAdd, reqStudentList, reqStudentById, reqStudentDelete, reqStudentUpdate } from "@/api/student";
import type {
    studentInfoData, studentListResponseData
    , studentResponseData, studentAllResponseData, studentListFormData
} from "@/api/student/type";
import type { StudentState } from "./types/types";
let useStudentStore = defineStore('Student', {
    state: (): StudentState => {
        return {
            students: []
        }
    },
    actions: {
        //获取所有学生
        async getStudentAll() {
            let result: studentAllResponseData = await reqStudentAll();
            if (result.status == 0) {
                this.students = (result.data as studentInfoData[])
                return 'ok'
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //分页获取学生
        async getStudentList(data: studentListFormData) {
            let result: studentListResponseData = await reqStudentList(data);
            if (result.status == 0) {
                this.students = (result.data?.data as studentInfoData[])
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //添加学生
        async addStudent(data: studentInfoData) {
            let result: studentResponseData = await reqStudentAdd(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //根据id查询学生
        async getStudentById(_id: string) {
            let result :studentResponseData= await reqStudentById(_id);
            if(result.status == 0){
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //更新学生信息
        async updateStudent(data: studentInfoData) {
            let result: studentResponseData = await reqStudentUpdate(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //删除学生
        async deleteStudent(studentId: string) {
            let result: studentResponseData = await reqStudentDelete(studentId);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        getters: {

        }
    },
})
export default useStudentStore
